package com.qf.wangqing.sportsnews.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.fragment.CompetitionFragment;import com.qf.wangqing.sportsnews.databinding.ActivityMainBinding;

import com.qf.wangqing.sportsnews.fragment.headline.HeadLineFragment;
import com.qf.wangqing.sportsnews.fragment.NewsFragment;
import com.qf.wangqing.sportsnews.fragment.SlidingFragment;
import com.qf.wangqing.sportsnews.fragment.headline.HeadLineFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private FragmentTransaction transcation;
    private FragmentManager manager;
    private  NewsFragment newsFragment;
    private  HeadLineFragment headLineFragment;
    private CompetitionFragment competitionFragment;
    private ActivityMainBinding binding;

    public ActivityMainBinding getBinding() {
        return binding;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
        initView();
        initFragment();
        initSliding();
    }

    private void initSliding() {
        SlidingFragment fragment = new SlidingFragment();
        FragmentTransaction transcation2=manager.beginTransaction().add(R.id.slidingfragment_main,fragment);
        transcation2.commit();
    }

    //初始化碎片
    private void initFragment() {
        newsFragment = new NewsFragment();
        headLineFragment = new HeadLineFragment();
        competitionFragment = new CompetitionFragment();

        manager = getSupportFragmentManager();
        transcation = manager.beginTransaction();
        transcation.add(R.id.fragment_main,newsFragment);
        transcation.add(R.id.fragment_main,headLineFragment);
        transcation.add(R.id.fragment_main,competitionFragment);

        transcation.hide(headLineFragment);
        transcation.hide(competitionFragment);
        transcation.commit();

    }

    private void initView() {
        binding.groupMain.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        transcation = manager.beginTransaction();
        switch (i){
            case R.id.news_btn_main:
                transcation.show(newsFragment);
                transcation.hide(competitionFragment);
                transcation.hide(headLineFragment);

                break;
            case R.id.headline_btn_main:
                transcation.show(headLineFragment);
                transcation.hide(competitionFragment);
                transcation.hide(newsFragment);

                break;
            case R.id.competition_btn_main:
                transcation.show(competitionFragment);
                transcation.hide(newsFragment);
                transcation.hide(headLineFragment);

                break;
        }
        transcation.commit();
    }


}
